import java.util.HashMap;
import java.util.Map;
/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Room end;
    private Player player;
    private Player[] playerN;
    private int t=0;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player("主角", 100, 8, 4);
        playerN = new Player[10];
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
       Room birth;
        Room[] next=new Room[11];
        String[] number=new String[]{"零","一","二","三","四","五","六","七","八","九","十"};
        String[] foodName=new String[]{" ","小回血瓶1","小回血瓶2","小回血瓶3","小回血瓶4","小回血瓶5","中回血瓶1","中回血瓶2","中回血3","大回血瓶1","大回血瓶2"};
        String[] monster=new String[]{" ","巨蚁","杀人虫","机械虫","侦察峰","生物炮虫","机械鸟","步枪鸟","喷火怪","机械河马","章鱼坦克"};
        int life=5;
        int hurt=5;
        int armor=5;
        int value=5;
        // create the rooms
        birth = new Room("门外");
        for(int i=1;i<11;i++) {
           next[i] = new Room("第"+number[i]+"个房间");
        }
        next[0] = new Room("你从第十重门走了出来，面前有一位老者:");
        
        for(int i=0;i<11;i++) {
            next[i].setFood(new Food(foodName[i] , "【用来回血（5，10，15）】", value));
            next[i].setPlayer(new Player(monster[i], life+i, hurt+i, armor+i));
            if(i==6) {
                value=10;
            }
            if(i==9) {
                value=15;
            }
        }
        
        
        // initialise room exits
        birth.setExit("next",next[1]);
        for(int i=1;i<10;i++) {
            next[i].setExit("next", next[i+1]);
        }
        next[10].setExit("next", next[0]);
        currentRoom = birth;  // start game outside
        end=next[0];
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("谢谢你的体验。再见。");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎来到祖尔的世界！");
        System.out.println("祖尔的世界是一个新的，令人难以置信的无聊的冒险游戏。");
        System.out.println("如果需要帮助，请键入“help”。");
        System.out.println();
        System.out.println("你在" + currentRoom.getDescription());
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("我不知道你的意思。。。");
            return false;
        }
        
        Food food = currentRoom.getFood();
        Word commandWord = command.getCommandWord();
        switch(commandWord) {
            case HELP:
                printHelp();
                break;
            case GO:
                int q=goRoom(command);
                if(q==0) {
                    wantToQuit = true;
                }
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case LOOK:
                
                if(food != null) {
                    System.out.println("房间里有"+food.getName()+food.getDescribe());
                }
                else {
                    System.out.println("房间里没有东西");
                }
                break;  
            case PICK:
                pickFood(food);
                break;
            case EAT:   
                eatFood(command);
                break;
            case CHECK:
                checkFood();
                break;
            case TALENT:
                if(t>0) {
                    plusTalent(command);
                }
        }
        
        
        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        Word[] words=Word.values();
        System.out.println("你在祖尔是一个新手冒险家，今年18岁，现在在参加十重门比赛");
        System.out.println("祖尔世界的传统习俗是只有通过了十重门的考验，并拿的冒险家独有的纹耀，才是一个真正的冒险家");
        System.out.println("十重门规则：每挑战门通过时输出”talent“就会有一个加点功能。");
        System.out.println("当成功挑战完十重门后你就会获得冒险家纹耀，成为真正的冒险家！");
        System.out.println();
        System.out.println("你在这个世界有以下几个指令可操作：");
        for(int i=0;i<words.length;i++) {
            System.out.print(words[i].getCommandWord()+" ");
        }
        //System.out.println("   go quit help");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private int goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪？");
            return 1;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.goNext(direction);
        
        if (nextRoom == null) {
            System.out.println("没有门！");
        }
        else if(nextRoom==end) {
            System.out.println(nextRoom.getDescription());
            System.out.println("年轻人，恭喜你完成了十重门的考验。");
            System.out.println("这是冒险者纹耀，以后你便是正式的冒险家了。");
            System.out.println();
            System.out.println("接下来，你只要不断的精进基本功力，并且研究个人的创意");
            System.out.println("创造出属于自己特有的能力，将自己的天资发挥到最大的境界");
            System.out.println("发挥出自己的独特性");
            System.out.println("想想自己的希望、自己的喜怒、");
            System.out.println("喜欢什么？想追求什么？到哪里去旅行？遇见什么样的人？得到什么经验？");
            System.out.println("就如同这一切的过程会形成你们的未来。也同样会展现最合适你们自身的能力。");
            System.out.println("你们必须要找到它，让它变的如影随形");
            System.out.println("你们只要想办法做到这一点");
            System.out.println("就一定可以指引你门往更好的未来之路前进");
            return 0;
        }
        else {
            currentRoom = nextRoom;
            System.out.println("你在" + currentRoom.getDescription());
            currentRoom.printExits();
            if(currentRoom.getPlayer()!=null) {
                return encounter();
            }
        }
        return 1;
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("放弃什么？");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    
    private void pickFood(Food food) {
        if(food!=null) {
            player.pick(food);
            System.out.println("拾取了"+food.getName());
            currentRoom.removeFood();
        }
        else {
            System.out.println("没有东西无法拾取");
        }
    }
    
    private void eatFood(Command command) {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("没有这个东西！");
            return;
        }

        String direction = command.getSecondWord();

        player.eat(direction);
        
    }
    
    private void checkFood() {
        player.check();
    }
    
    private int encounter() {
        Player enemy=currentRoom.getPlayer();
        System.out.println("你遭遇了"+enemy.getName());
        System.out.println("你和"+enemy.getName()+"打了起来！");
        int playerStrength=player.getStrength();
        int enemyStrength=enemy.getStrength();
        while(player!=null && enemy!=null) {
            int y1=player.getAggressivity()-enemy.getDefense();//我的伤害
            int y2=enemy.getAggressivity()-player.getDefense();//它的伤害
            if(y1<0) {
                y1=0;
            }
            if(y2<0) {
                y2=0;
            }
            playerStrength-=y2;
            player.setStrength(playerStrength);
            enemyStrength-=y1;
            System.out.println("你攻击了"+enemy.getName()+y1+"滴血！");
            System.out.println(enemy.getName()+"攻击了你，你损耗了"+y2+"滴血！");
            if(playerStrength<=0) {
                System.out.println("挑战失败！");
                
                return 0;
            }
            if(enemyStrength<=0) {
                enemy=null;
                currentRoom.setPlayer(enemy);
                System.out.println("挑战成功！");
                t++;
                return 1;
            }
            
        }
        return 1;
    }
    
    private void plusTalent(Command command) {
       String direction = command.getSecondWord();
       if(direction==null) {
           System.out.println("加点失败！");
           return;
        }
       
       if(direction.equals("攻击力")) {
          System.out.println("加点成功！");
          int aggressivity=player.getAggressivity()/4;
          player.setAggressivity(aggressivity);
        }
       
       if(direction.equals("防御力")) {
          System.out.println("加点成功！");
          int defense=player.getDefense()/4;
          player.setDefense(defense);
        }
       System.out.println("加点成功！");
       t--;
    }
}

